package com.zhilianbao.leyaogo.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.bql.statetypelayout.AnimationStateTypeLayout;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.app.LeYaoGoApplication;

/**
 * 用于设置 {@link AnimationStateTypeLayout}的加载状态,
 * 不仅可以用于{@link com.zhilianbao.leyaogo.http.callback.LoadingViewCallback}中
 * <p>
 * Created by gyt on 2016/12/28
 */
public class LoadStateUtils {

    private final View                     mView;
    private final Context                  mContext;
    private final String                   errorContent;
    private final String                   errorLeftButtonText;
    private final String                   errorRightButtonText;
    private final String                   emptyButtonText;
    private       Drawable                 errorNetDrawable;
    private       Drawable                 errorServerDrawable;
    private       AnimationStateTypeLayout mStateTypeLayout;
    private       View.OnClickListener     mOnRightClickListener;
    private       View.OnClickListener     mOnLeftClickListener;


    /**
     * @param context
     * @param view    fragment的rootView
     */
    public LoadStateUtils(Context context, View view) {
        this.mView = view;
        this.mContext = context;
        errorContent = LeYaoGoApplication.getContext().getString(R.string.net_error);
        errorLeftButtonText = LeYaoGoApplication.getContext().getString(R.string.re_load);
        errorRightButtonText = LeYaoGoApplication.getContext().getString(R.string.errorButtonRightPlaceHolder);
        emptyButtonText = emptyBtnText();
        errorNetDrawable = LeYaoGoApplication.getContext().getResources().getDrawable(R.drawable.no_network);
        errorServerDrawable = LeYaoGoApplication.getContext().getResources().getDrawable(R.drawable.failed_to_load);
        mOnLeftClickListener = v -> errorLeftOrEmptyBtnClick(v);
        mOnRightClickListener = v -> errorRightBtnClick();
        initStateLayout();
    }

    private void errorRightBtnClick() {
        Intent intent = null;
        //判断手机系统的版本  即API大于10 就是3.0或以上版本
        if (android.os.Build.VERSION.SDK_INT > 10) {
            intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
        } else {
            intent = new Intent();
            ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
            intent.setComponent(component);
            intent.setAction("android.intent.action.VIEW");
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        LeYaoGoApplication.getContext().startActivity(intent);
    }

    public void errorLeftOrEmptyBtnClick(View v) {

    }

    private void initStateLayout() {
        if (mStateTypeLayout == null) {
            if (mView != null) {
                mStateTypeLayout = (AnimationStateTypeLayout) mView.findViewById(R.id.stateLayout);
            } else if (mContext != null) {
                mStateTypeLayout = (AnimationStateTypeLayout) ((Activity) mContext).findViewById(R.id.stateLayout);
            }
            if (mStateTypeLayout == null) {
                throw new NullPointerException("Do you have defined a AnimationStateTypeLayout layout in your xml?");
            } else {
                //                SkinManager.getInstance().injectSkin(mStateTypeLayout);
            }

        }
    }


    /**
     * 显示加载视图
     */
    public void showLoading() {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.showLoading(/*loadingContent*/);
        }
    }


    /**
     * 显示空视图
     */
    public void showEmpty() {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.showEmpty(emptyDrawable(), null, emptyContent(), showEmptyButton() ? emptyButtonText : null, mOnLeftClickListener);
        }
    }


    /**
     * 显示空视图
     */
    public void showEmpty(String text) {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.showEmpty(emptyDrawable(), null, text, showEmptyButton() ? emptyButtonText : null, mOnLeftClickListener);
        }
    }


    /**
     * 设置错误视图
     */
    //    public void showError() {
    //        if (mStateTypeLayout != null) {
    //            mStateTypeLayout.showError(errorDrawable, null, errorContent, errorLeftButtonText, errorRightButtonText, mOnLeftClickListener, mOnRightClickListener);
    //        }
    //    }

    /**
     * 设置网络错误视图
     */
    public void showNetError() {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.showError(errorNetDrawable, null, LeYaoGoApplication.getContext().getString(R.string.net_error), errorLeftButtonText, null, mOnLeftClickListener, null);
        }
    }


    /**
     * 设置服务器错误视图
     */
    public void showServerError() {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.showError(errorServerDrawable, null, LeYaoGoApplication.getContext().getString(R.string.server_error), errorLeftButtonText, null, mOnLeftClickListener, null);
        }
    }


    /**
     * 显示内容
     */
    public void showContent() {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.showContent();
        }
    }

    /**
     * 隐藏所有
     */
    public void hideAllView() {
        if (mStateTypeLayout != null) {
            mStateTypeLayout.hideEmptyView();
            mStateTypeLayout.hideErrorView();
            mStateTypeLayout.hideLoadingView();
            mStateTypeLayout.hideContentView();
        }
    }

    /**
     * 是否显示空视图按钮  默认不显示
     *
     * @return
     */
    public boolean showEmptyButton() {
        return false;
    }

    /**
     * 是否显示服务器异常刷新按钮  默认显示
     *
     * @return
     */
    public boolean showErrorButton() {
        return true;
    }


    /**
     * 空图片 若需另行设置 请重写该方法
     *
     * @return
     */
    public Drawable emptyDrawable() {
        return LeYaoGoApplication.getContext().getResources().getDrawable(R.drawable.common_content_empty);
    }

    /**
     * 空内容文本 若需另行设置 请重写该方法
     *
     * @return
     */
    public String emptyContent() {
        return LeYaoGoApplication.getContext().getString(R.string.emptyContentPlaceholder);
    }

    public String emptyBtnText() {
        return LeYaoGoApplication.getContext().getString(R.string.re_load);
    }

}
